package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-30 12:20
 **/
public class LeetCode322 {

    public int coinChange(int[] coins, int amount) {
        if (amount == 0) {
            return 0;
        }
        //存储
        int[] f = new int[amount + 1];
        f[0] = 0;
        for (int i = 1; i < amount + 1; i++) {
            f[i] = Integer.MAX_VALUE;
            for (int i1 = 0; i1 < coins.length; i1++) {
                if (i - coins[i1] >= 0 && f[i - coins[i1]] != Integer.MAX_VALUE) {
                    f[i] = Math.min(f[i], f[i - coins[i1]] + 1);
                }
            }
        }

        return f[amount] == Integer.MAX_VALUE ? -1 : f[amount];
    }

}
